package demo.database.Ehcache;

import java.util.HashMap;

import org.ehcache.Cache;
import org.ehcache.CacheManager;
import org.ehcache.config.Builder;
import org.ehcache.config.CacheConfiguration;
import org.ehcache.config.ResourcePools;
import org.ehcache.config.builders.CacheConfigurationBuilder;
import org.ehcache.config.builders.CacheManagerBuilder;
import org.ehcache.config.builders.ResourcePoolsBuilder;
import org.ehcache.config.units.EntryUnit;
import org.ehcache.config.units.MemoryUnit;

import com.titan.core.utils.debug.Timer;
import com.titan.core.web.utils.SpringBootUtils;

public class Demo02_性能测试 {
    private static void run() throws Exception {
        int count = 10000000;
//        testHeap(count);        // 20秒620毫秒
//        testOffheap(count);     // 37秒265毫秒
//        testHashMap(count);     // 4秒187毫秒
    }
    
    private static void testHeap(int count) {
        CacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder().build();
        cacheManager.init();
        try {
            Cache<Integer, String> myCache = cacheManager.createCache("myCache", heap(Integer.class, String.class, count));
            Timer timer = Timer.$();
            for (int i = 0; i <= count; i++) {
                // 写
                myCache.put(i, "@" + i);
                // 读
                String value = myCache.get(i);
//                System.out.println("get at " + i + ":" + value);
            }
            timer.print("执行总用时");
        } finally {
            cacheManager.removeCache("myCache");
            cacheManager.close();
        }
    }
    private static void testOffheap(int count) {
        CacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder().build();
        cacheManager.init();
        try {
            Cache<Integer, String> myCache = cacheManager.createCache("myCache", offheap(Integer.class, String.class, 1024));
            Timer timer = Timer.$();
            for (int i = 0; i <= count; i++) {
                // 写
                myCache.put(i, "@" + i);
                // 读
                String value = myCache.get(i);
//                System.out.println("get at " + i + ":" + value);
            }
            timer.print("执行总用时");
        } finally {
            cacheManager.removeCache("myCache");
            cacheManager.close();
        }
    }    
    private static void testHashMap(int count) {
        HashMap<Integer, String> myCache = new HashMap<Integer, String>(count);
        try {
            Timer timer = Timer.$();
            for (int i = 0; i <= count; i++) {
                // 写
                myCache.put(i, "@" + i);
                // 读
                String value = myCache.get(i);
//                System.out.println("get at " + i + ":" + value);
            }
            timer.print("执行总用时");
        } finally {
            myCache.clear();
        }
    }    
    
    
    private static <K, V> CacheConfiguration<K, V> heap(Class<K> keyType, Class<V> valueType, long entries) {
        CacheConfigurationBuilder<K, V> configuration = CacheConfigurationBuilder
                .newCacheConfigurationBuilder(keyType, valueType, heap(entries));
        return configuration.build();
    }
    private static <K, V> CacheConfiguration<K, V> offheap(Class<K> keyType, Class<V> valueType, int mb) {
        CacheConfigurationBuilder<K, V> configuration = CacheConfigurationBuilder
                .newCacheConfigurationBuilder(keyType, valueType, offheap(mb));
        return configuration.build();
    }
    private static Builder<? extends ResourcePools> heap(long entries) {
        return ResourcePoolsBuilder.newResourcePoolsBuilder().heap(entries, EntryUnit.ENTRIES);
    }
    private static Builder<? extends ResourcePools> offheap(int mb) {
        return ResourcePoolsBuilder.newResourcePoolsBuilder().offheap(mb, MemoryUnit.MB);
    }

    public static void main(String[] args) {
        try {
            run();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            SpringBootUtils.shutdown();
        }
    }
}
